#include "mainscene.h"
#include "ui_mainscene.h"
#include "stone.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QMessageBox>
#include <firstscene.h>
#include <mypushbutton.h>
#include <QTimer>
#include <QMenuBar>
#include <QPushButton>

mainscene::mainscene(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::mainscene)
{
    ui->setupUi(this);
    //设置固定大小
    setFixedSize(550,700);
    //设置窗口图片
    setWindowIcon(QIcon(":/firstsence.jpg"));
    //设置标题
    setWindowTitle("下棋主场景");

    //初始化棋子状态
    for(int i=0;i<12;i++)
    {
        sto[i].id=i;
        sto[i].is_dead=false;
    }
    for(int i=0;i<4;i++)
    {
        sto[i].x = startx + i * d;
        sto[i].y = starty;
    }
    sto[4].x = startx;
    sto[4].y = starty + d;
    sto[5].x = startx + d * 3;
    sto[5].y = starty + d;

    sto[6].x = startx;
    sto[6].y = starty + d * 2;
    sto[7].x = startx + d * 3;
    sto[7].y = starty + d * 2;
    for(int i=8;i<12;i++)
    {
        sto[i].x = startx + d * (i-8);
        sto[i].y = starty + d * 3;
    }

    //返回按钮
    MyPushButton * backBtn = new MyPushButton(":/BackButton.png",":/BackButtonSelected.png");
    backBtn->setParent(this);
    //移动到右下角
    backBtn->move(this->width() - backBtn->width(),
                  this->height() - backBtn->height());
    //点击返回
    connect(backBtn,&MyPushButton::clicked,[=](){
        //告诉主场景，我返回了，主场景监听返回按钮
        //延时返回
        QTimer::singleShot(200,this,[=](){
            emit this->chooseSceneBack();
        });
    });

}

void mainscene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    //加载图片
    pix.load(":/play.jpg");
    //拉伸图片
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //图片太大，需要进行缩放操作
    pix=pix.scaled(pix.width()*1.1,pix.height()*1.1);

    //创建按钮
    QPushButton * btn1 = new QPushButton;
    btn1->setParent(this);
    btn1->setText("退出游戏");
    btn1->move(0,595);
    btn1->show();

    QPushButton * btn2 = new QPushButton;
    btn2->setParent(this);
    btn2->setText("详细规则");
    btn2->move(0,635);
    btn2->show();

    //点击按钮 实现退出游戏
    connect(btn1,&QPushButton::clicked,[=](){
        this->close();
    });

    learn_scene = new learn_rule;
    connect(btn2,&MyPushButton::clicked,[=](){
        QTimer::singleShot(100,this,[=](){
            //游戏场景隐藏
            this->hide();
            //显示学习规则的场景
            learn_scene->show();

            //监听学习规则场景的返回信号
            connect(learn_scene,&learn_rule::chooseSceneBack,[=](){
                    //学习规则的场景隐藏
                    learn_scene->hide();
                    //游戏场景回归
                    this->show();
                }
            );
        });
    });


    //画棋盘
    for(int i=0;i<5;i++)//横线
    {
        painter.drawLine(QPoint(startx,starty+d*i),QPoint(startx+d*4,starty+d*i));
    }
    for(int i=0;i<5;i++)//竖线
    {
        painter.drawLine(QPoint(startx+d*i,starty),QPoint(startx+d*i,starty+d*4));
    }


    //绘制12颗棋子
    //安排不同画笔、画刷
    QPen pen1(Qt::red);
    QBrush brush1(Qt::red);
    QPen pen2(Qt::green);
    QBrush brush2(Qt::green);
    QPen pen3(Qt::yellow);
    QBrush brush3(Qt::yellow);
    for(int i=0;i<6;i++)
    {
        if(sto[i].is_dead)
        {
            continue;
        }
        if(sto[i].id==selected_id)
        {
            painter.setPen(pen3);
            painter.setBrush(brush3);
        }
        else
        {
            painter.setPen(pen1);
            painter.setBrush(brush1);
        }
        painter.drawEllipse(sto[i].x+2,sto[i].y+2,80-4,80-4);
    }

    for(int i=6;i<12;i++)
    {
        if(sto[i].is_dead)
        {
            continue;
        }
        if(sto[i].id==selected_id)
        {
            painter.setPen(pen3);
            painter.setBrush(brush3);
        }
        else
        {
            painter.setPen(pen2);
            painter.setBrush(brush2);
        }
        painter.drawEllipse(sto[i].x+2,sto[i].y+2,80-4,80-4);
    }
}

void mainscene::mousePressEvent(QMouseEvent *event)
{
    int row,row2;
    int col,col2;
    if(event->button() == Qt::LeftButton)
    {
        row=(event->x()-startx)/d;
        col=(event->y()-starty)/d;
        row2=(sto[selected_id].x-startx)/d;
        col2=(sto[selected_id].y-starty)/d;
    }
    if(is_selected)//可以行棋
    {
        if(canMove(row,col) && canMove2(row,row2,col,col2))
        {
            sto[selected_id].x = row * 80 + startx;
            sto[selected_id].y = col * 80 + starty;
            if(lastid>=0 && lastid<6)
            {
                if(selected_id){}
            }
            //还原成未选状态
            is_selected = false;
            selected_id = -1;
        }
        else if(1)
        {
            //判断这个行列值上有无棋子
            for(int i=0;i<12;i++)
            {
                if((sto[i].x-startx)/d==row && (sto[i].y-starty)/d==col && sto[i].is_dead==false)
                {
                    selected_id = i;//记录下来
                    lastid = i;
                    is_selected = true;
                    update();
                }
            }
        }
        else
        {
            QMessageBox::warning(this,"规则错误","干啥，开挂呢？");
        }
        update();
    }
    else//没有棋子被选中
    {
        //判断这个行列值上有无棋子
        for(int i=0;i<12;i++)
        {
            if((sto[i].x-startx)/d==row && (sto[i].y-starty)/d==col && sto[i].is_dead==false)
            {
                selected_id = i;//记录下来
                lastid = i;
                is_selected = true;
                update();
            }
        }
    }
}

void mainscene::mouseReleaseEvent(QMouseEvent *event)
{
    //手动让棋子死亡
    if(event->button() == Qt::RightButton)
    {
        int row2=(event->x()-startx)/d;
        int col2=(event->y()-starty)/d;
        for(int i=0;i<12;i++)
        {
            if((sto[i].x-startx)/d==row2 && (sto[i].y-starty)/d==col2 && sto[i].is_dead==false)
            {
                if(QMessageBox::Yes==QMessageBox::question(this,"吃子","你是否要删除这颗棋子",QMessageBox::Yes | QMessageBox::No,QMessageBox::No))
                {
                    sto[i].is_dead = true;
                    update();
                }
            }
        }
    }
}

bool mainscene::canMove(int x,int y)
{
    if(x<0 || x>3 || y<0 || y>3)
    {
        return false;
    }
    for(int i=0;i<12;i++)
    {
        if((sto[i].x-startx)/d==x && (sto[i].y-starty)/d==y)
        {
            if(sto[i].is_dead==false)
            {
                return false;
            }
        }
    }
    return true;
}

bool mainscene::canMove2(int row,int row2,int col,int col2)
{
    if((row-row2)<-1 && (row-row2)>1)
    {
        return false;
    }
    else if((col-col2)<-1 && (col-col2)>1)
    {
        return false;
    }
    else if(row!=row2 && col!=col2)
    {
        return false;
    }
    return true;
}

bool mainscene::isWin()
{
    bool flag1=false;
    for(int i=0;i<6;i++)
    {
        if(!sto[i].is_dead)
        {
            flag1=true;
            break;
        }
    }
    bool flag2=false;
    for(int i=6;i<12;i++)
    {
        if(!sto[i].is_dead)
        {
            flag2=true;
            break;
        }
    }
    if(flag1==false || flag2==false)
    {
        return true;
    }
    return false;
}

mainscene::~mainscene()
{
    delete ui;
}
